using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using WpfApp_ChenxiConsole.TCP;

namespace WpfApp_ChenxiConsole.OTA
{
    /// <summary>
    /// TCP客户端选择对话框
    /// 用于在启动ECU升级窗口时选择要绑定的TCP客户端选项卡
    /// </summary>
    public partial class TcpClientSelectorDialog : Window
    {
        public TcpConsoleControl? SelectedTcpConsoleControl { get; private set; }

        public TcpClientSelectorDialog(List<(string TabHeader, TcpConsoleControl TcpConsole)> tcpClients)
        {
            InitializeComponent();
            this.Title = "选择TCP客户端";
            this.Width = 400;
            this.Height = 300;
            this.ResizeMode = ResizeMode.NoResize;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.Owner = Application.Current.MainWindow;
            
            // 绑定TCP客户端列表到ComboBox
            ClientListComboBox.ItemsSource = tcpClients;
            ClientListComboBox.DisplayMemberPath = "Item1"; // 使用Item1访问ValueTuple的第一个元素（TabHeader）
            
            // 如果有客户端，默认选择第一个
            if (tcpClients.Count > 0)
            {
                ClientListComboBox.SelectedIndex = 0;
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            if (ClientListComboBox.SelectedItem is ValueTuple<string, TcpConsoleControl> selectedItem)
            {
                SelectedTcpConsoleControl = selectedItem.Item2;
                DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("请选择一个TCP客户端", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }
    }
}